package com.kgc.ljp.server.dao.imp;

import com.kgc.ljp.server.entity.Admin;
import com.kgc.ljp.server.entity.User;
import com.kgc.ljp.server.entity.UserAndAdmin;
import com.kgc.ljp.server.util.Init;
import com.kgc.ljp.server.util.SetUtil;

import java.util.Map;

public class Login {
    static Map<String,User> userMap= null;
    static Map<String,Admin> adminMap= null;
    static {
        userMap = (Map<String, User>) SetUtil.readObj("User.txt");
        if(userMap==null){
            userMap = Init.userMap;
        }
        adminMap = (Map<String, Admin>) SetUtil.readObj("Admin.txt");
        if (adminMap==null){
            adminMap = Init.adminMap;
        }
        SetUtil.writeObj(userMap,"User.txt");
        SetUtil.writeObj(adminMap,"Admin.txt");
    }
    public static boolean userLogin(String name,String pwd){
        userMap = (Map<String, User>) SetUtil.readObj("User.txt");
        if(userMap==null){
            userMap = Init.userMap;
        }
        if (userMap.containsKey(name)){
            if (userMap.get(name).getPwd().equals(pwd)){
                if (Init.userMap.containsKey(name)){
                    System.out.println("此账号已被封禁");
                }
                return true;
            }else {
                return false;
            }
        } else{
            return false;
        }

    }




















    public static boolean adminLogin(String name,String pwd){
        if (adminMap.containsKey(name)){
            if (adminMap.get(name).getPwd().equals(pwd)){
                return true;
            }else {
                return false;
            }
        } else{
            return false;
        }
    }
}
